import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:package_info/package_info.dart';
import 'package:common/model/web_view_toast/web_view_toast.dart';
import 'package:common/common.dart';
import 'package:webview_flutter/webview_flutter.dart';

class TWebViewBloc extends Bloc<TWebViewEvent, TWebViewState> {
  TWebViewBloc(TWebViewState initialState) : super(initialState) {
    _currentState = initialState;
    add(_InitialEvent());
  }

  TWebViewState _currentState;
  WebViewController _webViewController;

  set webViewController(value) {
    _webViewController = value;
  }

  @override
  Stream<TWebViewState> mapEventToState(TWebViewEvent event) async* {
    print("TWebViewBloc: event=${event.runtimeType}");
    if (event is _InitialEvent) {
      PackageInfo packageInfo = await PackageInfo.fromPlatform();
      _currentState.packageInfo = packageInfo;
      yield _currentState.copy();
      return;
    }
    if (event is TWebViewTitleUpdateEvent) {
      String title = event.title;
      if (TUtil.isNotEmptyString(title)) {
        if (title.startsWith("\"") && title.endsWith("\"")) {
          title = title.substring(1, title.length - 1);
        }
        _currentState.title = title;
        yield _currentState.copy();
      }
      return;
    }

    if (event is TWebViewOnRequestUserInfoEvent) {
      _webViewController
          .evaluateJavascript(
              "flutterUserInfoResponse(${GlobalBlocs.get<UserBloc>(UserBloc.blocName).state.userInfo.systemUser.toJsonString()})")
          .catchError((e, s) {});
      return;
    }

    if (event is TWebViewCopyContentEvent) {
      var clipboardData = await Clipboard.getData(Clipboard.kTextPlain);//获取粘贴板 中的文本
      if (clipboardData != null) {
        _webViewController.evaluateJavascript("flutterGetCopy('${clipboardData?.text?.replaceAll('\n', '')??''}')");
      }
      return;
    }
      if (event is TWebViewScanCodeEvent) {
      _currentState.openScanCodePage = true;
      yield _currentState.copy();
      _currentState.openScanCodePage = false;
      return;
    }

    if (event is TWebViewOnTokenInvalidEvent) {
      print('TWebViewOnTokenInvalidEvent');
      GlobalBlocs.get(UserBloc.blocName).add(UserLogoutEvent());
      return;
    }
    if(event is TWebViewDutyManagerEvent){
      print('TWebViewDutyManagerEvent');
      GlobalBroadcast.send(GlobalDutyManagerEvent(event.map,event.context));
      return;
    }

    if (event is TWebViewToastEvent) {
      var toastLength = event.data.long ?? 1 == 1 ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
      var toastGravity = event.data.bottom ?? 1 == 1 ? ToastGravity.BOTTOM : ToastGravity.CENTER;
      var toastColor = event.data.error ?? 0 == 1 ? TColor.textError : TColor.primeColor;
      Fluttertoast.showToast(
          timeInSecForIosWeb: 3,
          msg: event.data.content,
          toastLength: toastLength,
          gravity: toastGravity,
          backgroundColor: toastColor,
          textColor: Colors.white);
      return;
    }

    if (event is TWebViewImagePickerEvent) {
      _currentState.showImagePickerDialog = true;
      yield _currentState.copy();
      _currentState.showImagePickerDialog = false;
      return;
    }
    if (event is TWebViewMultipleImagePickerEvent) {
      _currentState.showMultipleImagePickerDialog = true;
      yield _currentState.copy();
      _currentState.showMultipleImagePickerDialog = false;
      return;
    }
  }
}

class _InitialEvent extends TWebViewEvent {}

class TWebViewToastEvent extends TWebViewEvent {
  final WebViewToast data;

  TWebViewToastEvent(this.data);
}

class TWebViewDutyManagerEvent extends TWebViewEvent {
  Map<String, dynamic> map;
  BuildContext context;
  TWebViewDutyManagerEvent(this.map,this.context);
}


class TWebViewOnTokenInvalidEvent extends TWebViewEvent {}

class TWebViewOnRequestUserInfoEvent extends TWebViewEvent {}
class TWebViewCopyContentEvent extends TWebViewEvent {}
class TWebViewImagePickerEvent extends TWebViewEvent {}
class TWebViewMultipleImagePickerEvent extends TWebViewEvent {}
class TWebViewScanCodeEvent extends TWebViewEvent {}

class TWebViewTitleUpdateEvent extends TWebViewEvent {
  String title;

  TWebViewTitleUpdateEvent(this.title);
}

abstract class TWebViewEvent {}

class TWebViewState {
  String title = "";
  bool showImagePickerDialog = false;
  bool showMultipleImagePickerDialog = false;

  bool openScanCodePage = false;
  PackageInfo packageInfo;

  TWebViewState copy() {
    TWebViewState newState = TWebViewState();
    newState.title = title;
    newState.packageInfo = packageInfo;
    newState.openScanCodePage = openScanCodePage;
    newState.showImagePickerDialog = showImagePickerDialog;
    newState.showMultipleImagePickerDialog = showMultipleImagePickerDialog;
    return newState;
  }
}

enum LoadUrlType {
  URL,
  OTHER,
}
